//
//  NewViewController.m
//  網易
//
//  Created by 赵天宇 on 16/8/22.
//  Copyright © 2016年 赵天宇. All rights reserved.
//
//引用头文件
#import "AppDelegate.h"
#import "NewViewController.h"
#import "TouTiaoViewController.h"
#import "JingXuanViewController.h"
#import "YuLeViewController.h"
#import "TiYuViewController.h"
#import "WangYiHaoViewController.h"
#import "BeiJingViewController.h"
#import "ShiPinViewController.h"
#import "CaiJingViewController.h"
#import "KeJiViewController.h"
#import "QiCheViewController.h"
#import "ShiShangViewController.h"
#import "TuPianViewController.h"
#import "ZhiBoViewController.h"
#import "ReDianViewController.h"
#import "GenTieViewController.h"
#import "FangChanViewController.h"
#import "QingSongYiKeViewController.h"
#import "MainViewController.h"
@interface NewViewController ()<UIScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>{
    //    自定义导航条
    UIScrollView *titileV;
    //     滚动视图
    UIScrollView *myScrollV;
    //    存标题的数组
    NSArray *arr;
    //     存按钮tag值的数组
    NSMutableArray *tagM;
    //存放下方滚动视图的数组
    NSArray *arrV;
    //储存按钮的数组
    NSMutableArray *arrBtn;
    //自定义导航条右侧的＋号按钮
    UIButton *addBtn;
    //网格
    UICollectionView *collectionV;
    //页眉标题
    NSArray *arrHearder;
    BOOL aa;
    AppDelegate *app;
}
@end
#define WIDTH   self.view.frame.size.width
@implementation NewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    标题
    self.navigationItem.title=@"網易";
//    改变导航条标题字体大小
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:24]}];
    self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
//tagM初始化
    tagM = [NSMutableArray array];
//arrBtn初始化
    arrBtn = [NSMutableArray array];
//防止视图抖动（通用）
    self.automaticallyAdjustsScrollViewInsets=NO;
    //    隐藏导航条
    self.navigationController.navigationBar.barTintColor=[UIColor redColor];
    //    设置视图背景颜色
    self.view.backgroundColor = [UIColor whiteColor];
    //    给数组赋值
    arr = @[@"头条",@"精选",@"娱乐",@"体育",@"网易号",@"北京",@"视频",@"财经",@"科技",@"汽车",@"时尚",@"图片",@"直播",@"热点",@"跟贴",@"房产",@"轻松一刻"];
    //    加载view
    [self titleVinit];
    //    加载滚动视图
    [self myScrollVinit];
    //    加载按钮
    [self addBtn];
//     初始化视图控制器
    [self initController];
//    添加视图
    [self addView];
//  初始化按钮
    [self initAddBtn];
//   添加网格
    [self initCollectionV];
//    注册网格
    [collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"class"];
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"search_icon_highlighted@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(found)];
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    self.navigationItem.rightBarButtonItem=right;
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"night_nav_live_room_image@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(zhibo)];
    self.navigationItem.leftBarButtonItem=left;
//    注册页眉
    [collectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
//    页眉数据
    arrHearder=@[@"切换栏目",@"点击添加栏目"];
}
-(void)found{
    
}
//添加视图
-(void)addView{
      int ww=0;
    for (UIViewController *vv in arrV) {
        vv.view.frame=CGRectMake(ww*WIDTH, 0, self.view.frame.size.width, self.view.frame.size.height);
        [myScrollV addSubview:vv.view];
        ww++;
    }
}
//左侧直播按钮方法
-(void)zhibo{
    [[NSNotificationCenter defaultCenter]postNotificationName:@"ss" object:nil];
}
-(void)initController{
    TouTiaoViewController *c1 = [[TouTiaoViewController alloc]init];
    JingXuanViewController *c2 = [[JingXuanViewController alloc]init];
    YuLeViewController *c3 = [[YuLeViewController alloc]init];
    TiYuViewController *c4 = [[TiYuViewController alloc]init];
    WangYiHaoViewController *c5 = [[WangYiHaoViewController alloc]init];
    BeiJingViewController *c6 = [[BeiJingViewController alloc]init];
    ShiPinViewController *c7 = [[ShiPinViewController alloc]init];
    CaiJingViewController *c8 = [[CaiJingViewController alloc]init];
    KeJiViewController *c9 = [[KeJiViewController alloc]init];
    QiCheViewController *_c1 = [[QiCheViewController alloc]init];
    ShiShangViewController *_c2 = [[ShiShangViewController alloc]init];
    TuPianViewController *_c3 = [[TuPianViewController alloc]init];
    ZhiBoViewController *_c4 = [[ZhiBoViewController alloc]init];
    ReDianViewController *_c5 = [[ReDianViewController alloc]init];
    GenTieViewController *_c6 = [[GenTieViewController alloc]init];
    FangChanViewController *_c7 = [[FangChanViewController alloc]init];
    QingSongYiKeViewController *_c8 = [[QingSongYiKeViewController alloc]init];
    arrV=@[c1,c2,c3,c4,c5,c6,c7,c8,c9,_c1,_c2,_c3,_c4,_c5,_c6,_c7,_c8];
}
//初始化视图上方的滚动视图
-(void)titleVinit{
    titileV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width-30, 38)];
    titileV.backgroundColor = [UIColor whiteColor];
    titileV.bounces=NO;
    titileV.scrollEnabled=YES;
    titileV.tag=1;
    titileV.alwaysBounceHorizontal=YES;
    titileV.showsVerticalScrollIndicator=NO;
    titileV.showsHorizontalScrollIndicator=NO;
    [self.view addSubview:titileV];
}
// 初始化网格
-(void)initCollectionV{
    UICollectionViewFlowLayout *layout  = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake(60, 60);
    layout.minimumInteritemSpacing=5;
    layout.minimumLineSpacing=10;
    layout.headerReferenceSize=CGSizeMake(200, 50);
    collectionV =[ [UICollectionView alloc]initWithFrame:CGRectMake(0, 102, self.view.frame.size.width , 1) collectionViewLayout:layout];
    collectionV.delegate=self;
    collectionV.dataSource=self;
    collectionV.userInteractionEnabled=YES;
    collectionV.backgroundColor = [UIColor whiteColor ];
    collectionV.alpha=0.8;
    collectionV.hidden=YES;
    [self.view addSubview:collectionV];
}
//每个分区有多少个单元格
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return arr.count-1;
}
//返回单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"class" forIndexPath:indexPath];
    for (UIView *vv in cell.contentView.subviews) {
        [vv removeFromSuperview];
    }
//    cell.backgroundColor = [UIColor grayColor];
    UIButton *btn = [UIButton buttonWithType:0];
    btn.userInteractionEnabled=YES;
    btn.layer.masksToBounds=YES;
    btn.layer.cornerRadius=10;
    btn.frame = CGRectMake(5, 5, 60, 60);
    [btn setTitle:arr[indexPath.row] forState:0];
    btn.tag=indexPath.row;
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor grayColor];
    [cell.contentView addSubview:btn];
    return cell;
}
//分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if(kind==UICollectionElementKindSectionHeader){
        UICollectionReusableView *vv = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        for (UIView *cc in vv.subviews) {
            [cc removeFromSuperview];
        }
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
        lab.text=arrHearder[indexPath.section ];
        [vv addSubview:lab];
        return vv;
    }else{
        return nil;
    }
}
//初始化视图下方的滚动视图
-(void)myScrollVinit{
    myScrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 102, self.view.frame.size.width, self.view.frame.size.height)];
    myScrollV.pagingEnabled=YES;
    myScrollV.delegate=self;
    myScrollV.bounces=NO;
    myScrollV.tag=2;
    myScrollV.contentSize=CGSizeMake(WIDTH *arr.count, self.view.frame.size.height);
    myScrollV.backgroundColor = [UIColor greenColor];
    [self.view addSubview:myScrollV];
}
//按钮初始化
-(void)initAddBtn{
    addBtn = [UIButton buttonWithType:0];
    addBtn.frame = CGRectMake(self.view.frame.size.width-30, 68, 30, 30);
    [addBtn setTitle:@"" forState:0];
    [addBtn setBackgroundImage:[UIImage imageNamed:@"night_top_navigation_normal@2x.png"] forState:0];
    [addBtn addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
}
//点击按钮方法
-(void)addClick:(UIButton *)sender{
    aa=!aa;
    if(aa){
        collectionV.hidden=NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    addBtn.transform=CGAffineTransformRotate(addBtn.transform,-M_2_PI*1.4);
    collectionV.frame=CGRectMake(0, 102, self.view.frame.size.width, self.view.frame.size.height);
    [UIView commitAnimations];
    }else{
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        addBtn.transform=CGAffineTransformRotate(addBtn.transform,M_2_PI*1.4);

        collectionV.frame=CGRectMake(0, 102, self.view.frame.size.width, 1);
        [UIView commitAnimations];
        collectionV.hidden=YES;
    }
}
CGFloat x = 0.0;
//循环添加按钮
-(void)addBtn{
    for (int i = 0; i < arr.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.tag = i;
        [arrBtn addObject:btn];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        //通过监听，获取文字的frame
        CGRect rect = [arr[i] boundingRectWithSize:CGSizeMake(100, 38) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:btn.titleLabel.font} context:nil];
        btn.frame = CGRectMake(x, 0, rect.size.width+10, 38);
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [titileV addSubview:btn];
        x = (x+rect.size.width+30);
        if (!i) {
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }else{
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    titileV.contentSize = CGSizeMake(x, 38);
}
//点击按钮的方法
-(void)clickBtn:(UIButton *)sender{
    collectionV.hidden=YES;
    NSNumber*num=[NSNumber numberWithInteger:sender.tag];
    if ([tagM containsObject:num]) {
        [tagM removeAllObjects];
    }else{
        for (UIView *vv in titileV.subviews) {
            if ([vv isKindOfClass:[UIButton class]]) {
                UIButton*btn=(UIButton*)vv;
                if (btn.tag==sender.tag) {
                    myScrollV.contentOffset=CGPointMake(btn.tag*WIDTH, 0);
                    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                    [UIView beginAnimations:nil context:nil];
                    [UIView setAnimationDuration:0.25];
                    btn.titleLabel.font=[UIFont systemFontOfSize:18];
                    if (x-sender.frame.origin.x<self.view.frame.size.width/2) {
                        titileV.contentOffset=CGPointMake(x-self.view.frame.size.width, 0);
                    }else if(sender.frame.origin.x<self.view.frame.size.width/2){
                        titileV.contentOffset=CGPointMake(0, 0);
                    }else{
                        titileV.contentOffset=CGPointMake(sender.center.x-self.view.frame.size.width/2, 0);
                    }
                    [UIView commitAnimations];
                }else{
                    [UIView beginAnimations:nil context:nil];
                    [UIView setAnimationDuration:0.25];
                    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    btn.titleLabel.font=[UIFont systemFontOfSize:14];
                    [UIView commitAnimations];
                }
            }
        }
    }
}
// 滚动视图协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView.tag==2){
        for (UIButton *btn  in arrBtn) {
            if(btn.tag==scrollView.contentOffset.x/WIDTH){
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.25];
                btn.titleLabel.font=[UIFont systemFontOfSize:18];
                [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                [UIView commitAnimations];
            }else{
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:0.25];
                btn.titleLabel.font=[UIFont systemFontOfSize:14];
                [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                [UIView commitAnimations];
            }
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
